//
//  HJComposeToolBar.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/25.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

//按钮类型枚举
enum HJComposeToolBarButtonType:Int {
    
    case Picture = 0, Mention,Trend,Emoticon,Add

}

class HJComposeToolBar: UIStackView {
    
    var buttonClickClosure:((type:HJComposeToolBarButtonType)->())?
    var isSystemKeyboard:Bool = true{
        didSet{
            let button = self.viewWithTag(HJComposeToolBarButtonType.Emoticon.rawValue) as? UIButton
            var name = "compose_keyboardbutton_background"
            if isSystemKeyboard {
                //显示笑脸
                name = "compose_emoticonbutton_background"
            }
            button?.setImage(UIImage(named: name), forState: UIControlState.Normal)
            button?.setImage(UIImage(named: "\(name)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        distribution = .FillEqually
        addChildButton("compose_toolbar_picture",type: .Picture)
        addChildButton("compose_mentionbutton_background",type: .Mention)
        addChildButton("compose_trendbutton_background",type: .Trend)
        addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        addChildButton("compose_add_background",type: .Add)

    }
    
    private func addChildButton(imageName:String, type:HJComposeToolBarButtonType){
        let button = UIButton()
        button.addTarget(self, action: "buttonDidClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = type.rawValue
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addArrangedSubview(button)
    }
    
    @objc private func buttonDidClicked(sender:UIButton){
        
        buttonClickClosure?(type: HJComposeToolBarButtonType(rawValue: sender.tag)!)
    }
}
